<template>
  <div class="restock-container">
    <el-card>
      <el-form :inline="true"
               ref="search_data"
               class="demo-form-inline">
        <el-row>
          <el-form-item label="商品">
            <el-input v-model="search_data.title"
                      placeholder="输入商品名称" />
          </el-form-item>
          <el-form-item label="价格范围">
            <el-col :span="11">
              <el-form-item prop="date1">
                <el-input v-model="search_data.min_price"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="line"
                    :span="2">-</el-col>
            <el-col :span="11">
              <el-form-item prop="date2">
                <el-input v-model="search_data.max_price"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"
                       @click="onSearch">查询</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-card>

    <el-card shadow="never"
             style="margin-top:10px;">
      <div class="main-card">
        <div>
          <span style="font-size:18px;font-weight:900;">进货单</span>
        </div>
        <div>
          <el-button icon="el-icon-plus"
                     type="primary"
                     @click="handleAdd()">新增</el-button>
        </div>
      </div>
      <el-table :data="table_data"
                tooltip-effect="dark"
                style="width: 100%">
        <el-table-column label="进货单名称">
          <template slot-scope="scope">
            <div>{{scope.row.with_restock.title}}</div>
          </template>
        </el-table-column>
        <el-table-column label="客户">
          <template slot-scope="scope">
            <div>{{scope.row.with_customer.title}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="price"
                         label="售价">
        </el-table-column>
        <el-table-column prop="weight"
                         label="售出">
        </el-table-column>
        <el-table-column label="单价">
          <template slot-scope="scope">
            <div>{{scope.row.weight==0?0:(scope.row.price/scope.row.weight).toFixed(2)}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="sell_time"
                         label="售出日期">
        </el-table-column>
        <el-table-column label="操作"
                         width="100">
          <template slot-scope="scope">
            <el-tooltip class="item"
                        effect="dark"
                        content="删除"
                        placement="top-start">
              <i class="el-icon-delete"
                 @click="handleDelete(scope.row.id)"
                 style="margin-left:5px"></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

  </div>
</template>
<script>
import { getList, addData, delData } from '@/api/shipment'
import { getList as getCustomerList } from '@/api/customer'
export default {
  data () {
    return {
      search_data: {
        page: 1,
        limit: 10,
        title: '',
        min_price: '',
        max_price: '',
      },
      all_count: 0,
      table_data: [],
      customer_data: [],
      dialogFormVisible: false,
      dialogShipmentVisible: false,
    }
  },
  created () {
    this.fetchData()
    this.fetchCustomerData()
  },
  methods: {
    onSearch () { },
    handleAdd () {
      this.dialogFormVisible = true
    },

    handleDelete (id, index) {
      this.$confirm('此操作将永久删除该售出记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delData({ id: id }).then(response => {
          if (response.code == 1000) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.table_data.splice(index, 1)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleAddData () {
      if (this.form.id) {
        editData(this.form).then(response => {
          if (response.code == 1000) {
            this.$message({
              message: '数据保存成功',
              type: 'success'
            });
            this.dialogFormVisible = false
          } else {
            this.$message.error(response.msg);
          }
        })
      } else {
        addData(this.form).then(response => {
          if (response.code == 1000) {
            this.$message({
              message: '数据添加成功',
              type: 'success'
            });
            this.dialogFormVisible = false
            this.fetchData()
          } else {
            this.$message.error(response.msg);
          }
        })
      }
    },
    fetchData () {
      getList(this.search_data).then(response => {
        this.table_data = response.data.data
        this.all_count = response.data.count
      })
    },
    fetchCustomerData () {
      getCustomerList().then(response => {
        this.customer_data = response.data.data
      })
    }
  }
}
  </script>

  <style lang="scss"
         scoped>
.restock-container {
  max-width: 1300px;
  margin: 0 auto;
  padding: 30px;
  .main-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
